*** Settings ***
Documentation    TC13-Reports Generation AIR
Resource    ../../Keywords/Common_Head.robot
Resource    ../../Keywords/Common/Common_kw.robot
Force Tags    P3
Metadata    Author    Lynn
Metadata    ID    31051
Test Teardown    Run Keywords    Run Keyword If Test Failed    Take Screenshot In Allure
...    AND    Reload
...    AND    CloseSpace


*** Test Cases ***
TC13-Reports Generation AIR
    Step 1 User Goes To Reports Success Ratio Report
    Step 2 Select RFQ Launch Date From 2022 01 01
    Step 3 Go To Rate Comparison Report
    Step 4 Select RFQ Launch Date From 2024 01 01 in Rate Comparison Report
    Step 5 Go To Deadline Time Report
    Step 6 Select RFQ Launch Date From 2024 01 01 in Deadline Time Report
    Step 7 Click On Download Button
    Step 8 Go To Transit Time Report
    Step 9 Select RFQ Launch Date From 2024 01 01 in Transit Time Report


*** Keywords ***
Step ${step} User Goes To Reports Success Ratio Report
    #************************Step 1******************************
    #  Do:
    #      User goes to Reports Success Ratio Report
    #  Expected:
    #      Success Ratio Report page is displayed as index php attachments 
    #************************************************************
    Login Space
    Click    ${navigation_Reports}
    Click    ${navigation_Success_Ratio_Report}
    Wait For Elements State    ${title_Success_Ratio_Report}    enabled    60
    Wait For Elements State    ${report_Air}
    Wait For Elements State    ${report_FCL}
    Wait For Elements State    ${report_LCL}
    Wait For Elements State    ${report_upload}
    Wait For Elements State    ${report_RFQ_Date_Type}   
    Wait For Elements State    ${report_Basic_Currency}    
    Wait For Elements State    ${report_vertical_Market}   
    Wait For Elements State    ${report_customer}    
    Wait For Elements State    ${report_origin_Region}    
    Wait For Elements State    ${report_destination_Region}  
    Wait For Elements State    ${report_origin_Cluster}   
    Wait For Elements State    ${report_destination_Cluster}   
    Wait For Elements State    ${report_origin_Country}    
    Wait For Elements State    ${report_destination_Country}    
    Wait For Elements State    ${report_origin_Airport} 
    Wait For Elements State    ${report_destination_Airport}   
    Wait For Elements State    ${report_origin_Gateway}   
    Wait For Elements State    ${report_destination_Gateway}   
    Wait For Elements State    ${report_service_Level}  
    Wait For Elements State    ${report_Reset}   
    Wait For Elements State    ${report_General_Report_Checkbox} 
    Wait For Elements State    ${report_Detailed_Report_Checkbox}    
    Wait For Elements State    ${report_Generate_Report}   


Step ${step} Select RFQ Launch Date From 2022 01 01
    #************************Step 2******************************
    #  Do:
    #      Select RFQ Launch Date from 2022 01 01
    #      Select General Report Detailed Report
    #      Click on Generate Report
    #      Check the downloaded file
    #  Expected:
    #      After a few minutes the file is downloaded like
    #     attached
    #************************************************************
    select options by    ${report_RFQ_Date_Type}     text    RFQ Launch Date
    Fill Text    ${report_RFQ_Date_From}    01/01/2022
    # Comment: For limit size of report, set Date_To as 01/03/2022
    Fill Text    ${report_RFQ_Date_To}    01/03/2022
    Click    ${report_General_Report_Checkbox}
    Click    ${report_Detailed_Report_Checkbox}   
    ${DOWNLOAD_PATH}     Normalize Path    ${DOWNLOAD_PATH}
    Empty Directory    ${DOWNLOAD_PATH}
    ${filename}    Set Variable    ${DOWNLOAD_PATH}${/}report.xlsx
    Set Test Variable    ${filename}    ${filename}
    ${dl_promise}    Promise To Wait For Download    ${filename}
    Wait Until Keyword Succeeds    10x    400ms    Click     ${report_Generate_Report}
    Sleep    30s
    Wait Until Keyword Succeeds    20x    3s        Directory Should Not Be Empty    ${DOWNLOAD_PATH}
    Wait Until Keyword Succeeds    10x    400ms    Copy File    ${DOWNLOAD_PATH}${/}*    ${filename}
    Open Excel Document     ${filename}    General Report AIR
    Sleep    10s
    ${column_C}    Read Excel Column    col_num=3    sheet_name=General Report AIR
    ${column_D}    Read Excel Column    col_num=4    row_offset=5    max_num=5    sheet_name=General Report AIR
    ${column_F}    Read Excel Column    col_num=6    sheet_name=General Report AIR
    ${column_G}    Read Excel Column    col_num=7        row_offset=5    max_num=5    sheet_name=General Report AIR
    ${column_I}    Read Excel Column    col_num=9    sheet_name=General Report AIR
    ${column_J}    Read Excel Column    col_num=10        row_offset=5    max_num=5    sheet_name=General Report AIR
    ${column_L}    Read Excel Column    col_num=11    sheet_name=General Report AIR
    ${Row_1}    Read Excel Row    1    sheet_name=Detailed Report AIR
    Remove Values From List    ${Row_1}    ${None}
    Close All Excel Documents

    Open Excel Document    ${CURDIR}${/}TestData${/}AIR Success Ratio_Reporting_08162024_l8.xlsx    0
    Sleep    10s
    ${exp_column_C}    Read Excel Column    col_num=3    sheet_name=General Report AIR
    ${exp_column_D}    Read Excel Column    col_num=4    row_offset=5    max_num=5    sheet_name=General Report AIR
    ${exp_column_F}    Read Excel Column    col_num=6    sheet_name=General Report AIR
    ${exp_column_G}    Read Excel Column    col_num=7        row_offset=5    max_num=5    sheet_name=General Report AIR
    ${exp_column_I}    Read Excel Column    col_num=9    sheet_name=General Report AIR
    ${exp_column_J}    Read Excel Column    col_num=10        row_offset=5    max_num=5    sheet_name=General Report AIR
    ${exp_column_L}    Read Excel Column    col_num=11    sheet_name=General Report AIR
    ${exp_Row_1}    Read Excel Row    1    sheet_name=Detailed Report AIR

    Lists Should Be Equal    ${column_C}    ${exp_column_C}
    Lists Should Be Equal    ${column_D}    ${exp_column_D}
    Lists Should Be Equal    ${column_F}    ${exp_column_F}
    Lists Should Be Equal    ${column_G}    ${exp_column_G}
    Lists Should Be Equal    ${column_I}    ${exp_column_I}
    Lists Should Be Equal    ${column_J}    ${exp_column_J}
    Lists Should Be Equal    ${column_L}    ${exp_column_L}
    Lists Should Be Equal    ${Row_1}    ${exp_Row_1}
    Close All Excel Documents
    Remove File    ${filename}
    Empty Directory    ${DOWNLOAD_PATH}

Step ${step} Go To Rate Comparison Report
    #************************Step 3******************************
    #  Do:
    #      Go to Rate Comparison Report
    #  Expected:
    #      The page is displayed like
    #      index php attachments
    #************************************************************
    Click    ${navigation_Rate_Comparison_Report}
    Wait For Elements State    ${title_Rate_Comparison_Report}    enabled    60
    Wait For Elements State    ${report_Air}
    Wait For Elements State    ${report_FCL}
    Wait For Elements State    ${report_LCL}
    Wait For Elements State    ${report_upload}
    Wait For Elements State    ${report_RFQ_Date_Type}   
    Wait For Elements State    ${report_Rate_Comparison_Basic_Currency}  
    Wait For Elements State    ${report_vertical_Market}   
    Wait For Elements State    ${report_customer}    
    Wait For Elements State    ${report_origin_Region}    
    Wait For Elements State    ${report_destination_Region}  
    Wait For Elements State    ${report_origin_Cluster}   
    Wait For Elements State    ${report_destination_Cluster}   
    Wait For Elements State    ${report_origin_Country}    
    Wait For Elements State    ${report_destination_Country}    
    Wait For Elements State    ${report_origin_Airport} 
    Wait For Elements State    ${report_destination_Airport}   
    Wait For Elements State    ${report_origin_Gateway}   
    Wait For Elements State    ${report_destination_Gateway}   
    Wait For Elements State    ${report_service_Level}  
    Wait For Elements State    ${report_Reset}
    # Customer Data tab in Rate Comparison Report
    Wait For Elements State    ${report_Rate_Comparison_Customer_Data_Tab}   
    Wait For Elements State    ${report_Rate_Comparison_Origin_Charges_Tab}   
    Wait For Elements State    ${report_Rate_Comparison_Airfreight_and_Charges_Tab}    
    Wait For Elements State    ${report_Rate_Comparison_Destination_Charges_Tab}    
    Wait For Elements State    ${report_Rate_Comparison_Airfreight_and_Charges_Tab}  
    Wait For Elements State    ${report_Rate_Comparison_Lane_Number}   
    Wait For Elements State    ${report_Rate_Comparison_Postal_Code} 
    Wait For Elements State    ${report_Rate_Comparison_Commodity}    
    Wait For Elements State    ${report_Rate_Comparison_Volume_as_per_validity_period} 
    Wait For Elements State    ${report_Rate_Comparison_Remarks}    
    Wait For Elements State    ${report_Rate_Comparison_Remarks}   
    Wait For Elements State    ${report_Rate_Comparison_Customer_Target_Information}   
    Wait For Elements State    ${report_Rate_Comparison_Service Level Customer}    
    Wait For Elements State    ${report_Rate_Comparison_Description}  
    Wait For Elements State    ${report_Generate_Report} 
    # Origin Charges tab in Rate Comparison Report
    Click    ${report_Rate_Comparison_Origin_Charges_Tab}  
    Wait For Elements State    ${report_Rate_Comparison_Pick_up_Charges}   
    # Airfreight and Charges tab in Rate Comparison Report
    Click    ${report_Rate_Comparison_Airfreight_and_Charges_Tab}   
    Wait For Elements State    ${report_Rate_Comparison_Airfreight_Rate}   
    # Destination Linehaul tab in Rate Comparison Report
    Click    ${report_Rate_Comparison_Destination_Linehaul_Tab}    
    Wait For Elements State    ${report_Rate_Comparison_In-Transit_Handling}    
    Wait For Elements State    ${report_Rate_Comparison_Destination Linehaul}   
    # Destination Charges tab in Rate Comparison Report
    Click    ${report_Rate_Comparison_Destination_Charges_Tab}   
    Wait For Elements State    ${report_Rate_Comparison_Delivery_Charges}    
    Wait For Elements State    ${report_Rate_Comparison_Delivery_Transit_Time_in_hours}    

Step ${step} Select RFQ Launch Date From 2024 01 01 in Rate Comparison Report
    #************************Step 4******************************
    #  Do:
    #      Select RFQ Launch Date from 2024 01 01
    #      Select all checkboxes
    #      Click on Generate Report button
    #      Check the downloaded file
    #  Expected:
    #      After a few minutes the file is downloaded like attached
    #************************************************************
    Click    ${report_Rate_Comparison_Customer_Data_Tab}      
    Check All Check Box for Page    //section[@class="group ng-scope"] 
    # Check All Check Box for Report    group ng-scope
    ${uncheckedStatus}    Run Keyword And Return Status    Get Element States    ${report_Rate_Comparison_Delivery_Charges}    contains    unchecked
    IF    '${uncheckedStatus}' == 'True'
        Click    ${report_Rate_Comparison_Delivery_Charges}
    ELSE
        Repeat Keyword    2    Click    ${report_Rate_Comparison_Delivery_Charges}
    END
    ${uncheckedStatus}    Run Keyword And Return Status    Get Element States    ${report_Rate_Comparison_FTL_Full_Truck_Load}    contains    unchecked
    IF    '${uncheckedStatus}' == 'True'
        Click    ${report_Rate_Comparison_FTL_Full_Truck_Load}
    ELSE
        Repeat Keyword    2    Click    ${report_Rate_Comparison_FTL_Full_Truck_Load}
    END
    select options by    ${report_RFQ_Date_Type}     text    RFQ Launch Date
    Fill Text    ${report_RFQ_Date_From}    01/01/2024
    # Comment: For limit size of report, set Date_To as 01/02/2024
    Fill Text    ${report_RFQ_Date_To}    01/02/2024
    ${DOWNLOAD_PATH}     Normalize Path    ${DOWNLOAD_PATH}
    ${report_download}    Set Variable    ${DOWNLOAD_PATH}${/}report_step4.xlsx
    Set Test Variable    ${report_download}    ${report_download}
    ${dl_promise}    Promise To Wait For Download    ${filename}
    Wait Until Keyword Succeeds    10x    400ms    Click     ${report_Generate_Report}
    Sleep    60s
    Wait Until Keyword Succeeds    20x    3s        Directory Should Not Be Empty    ${DOWNLOAD_PATH}
    Wait Until Keyword Succeeds    10x    2s    Copy File    ${DOWNLOAD_PATH}${/}*    ${report_download}
    ${exp_report}    Set Variable    ${CURDIR}${/}TestData${/}Step4_AIR Rate Comparison Report08162024-073949.xlsx    
    Compare Excel Value    ${report_download}    ${exp_report}    174    4    sheetName=AIR_D    startRow=2
    Close All Excel Documents
    Empty Directory    ${DOWNLOAD_PATH}


Step ${step} Go To Deadline Time Report
    #************************Step 5******************************
    #  Do:
    #      Go to Deadline Time Report
    #  Expected:
    #      The page is displayed like
    #      index php attachments 
    #************************************************************
    Click    ${navigation_Deadline_Time_Report}
    Wait For Elements State    ${title_Deadline_Time_Report}    enabled    10s
    Wait For Elements State    ${report_Air}
    Wait For Elements State    ${report_FCL}
    Wait For Elements State    ${report_LCL}
    Wait For Elements State    ${report_RFQ_Date_Type}     
    Wait For Elements State    ${report_vertical_Market}   
    Wait For Elements State    ${report_customer}    
    Wait For Elements State    ${report_Deadline_Time_Tender}    
    Wait For Elements State    ${report_Deadline_Time_region}    
    Wait For Elements State    ${report_Deadline_Time_country}    
    Wait For Elements State    ${report_Deadline_Time_Tender_Level_Label}    
    Wait For Elements State    ${report_Deadline_Time_Global_Checkbox}    
    Wait For Elements State    ${report_Deadline_Time_Region_Checkbox}    
    Wait For Elements State    ${report_Deadline_Time_Country_Checkbox}   
    Wait For Elements State    ${report_Deadline_Time_Cluster} 
    Wait For Elements State    ${report_Reset}  
    Wait For Elements State    ${report_Search}  


Step ${step} Select RFQ Launch Date From 2024 01 01 in Deadline Time Report
    #************************Step 6******************************
    #  Do:
    #      Select RFQ Launch Date from 2024 01 01
    #      Click on Search
    #  Expected:
    #      After a few seconds the page displays like
    #      index php attachments 
    #************************************************************
    select options by    ${report_RFQ_Date_Type}     text    RFQ Launch Date
    Fill Text    ${report_RFQ_Date_From}    01/01/2024
    # Comment: For limit size of report, set Date_To as 01/02/2024
    Fill Text    ${report_RFQ_Date_To}    01/02/2024
    Click    ${report_Search} 
    Sleep    10s
    Wait Until Keyword Succeeds    20x    3s    Check Search Result Count in Deadline Time Report

Step ${step} Click On Download Button
    #************************Step 7******************************
    #  Do:
    #      Click on Download button
    #  Expected:
    #      After a few seconds the file is downloaded like attached
    #************************************************************
    Empty Directory    ${DOWNLOAD_PATH}
    ${DOWNLOAD_PATH}     Normalize Path    ${DOWNLOAD_PATH}
    ${report_download}    Set Variable    ${DOWNLOAD_PATH}${/}report_step7.xlsx
    Set Test Variable    ${report_download}    ${report_download}
    ${dl_promise}    Promise To Wait For Download    ${report_download}
    Wait Until Keyword Succeeds    10x    400ms    Click     ${report_Download_Button}
    Sleep    60s
    Wait Until Keyword Succeeds    20x    3s        Directory Should Not Be Empty    ${DOWNLOAD_PATH}
    # Wait Until Keyword Succeeds    10x    400ms    Copy File    ${DOWNLOAD_PATH}${/}*    ${report_download}
    ${exp_report}    Set Variable    ${CURDIR}${/}TestData${/}Step7_AIR Deadline Report _08162024_kh.xlsx   
    Compare Excel Value    ${report_download}    ${exp_report}    14    4    startRow=5    startColumn=3
    Compare Excel Value    ${report_download}    ${exp_report}    17    13    startRow=17    startColumn=3
    Close All Excel Documents
    Empty Directory    ${DOWNLOAD_PATH}


Step ${step} Go To Transit Time Report
    #************************Step 8******************************
    #  Do:
    #      Go to Transit Time Report
    #  Expected:
    #      The page is displayed like
    #      index php attachments 
    #************************************************************
    Click    ${navigation_Transit_Time_Report}
    Wait For Elements State    ${title_Transit_Time_Report}    enabled    10s
    Wait For Elements State    ${report_Air}
    Wait For Elements State    ${report_FCL}
    Wait For Elements State    ${report_LCL}
    Wait For Elements State    ${report_RFQ_Date_Type}
    Wait For Elements State    ${report_tender_status}
    Wait For Elements State    ${report_vertical_Market}      
    Wait For Elements State    ${report_customer}    
    Wait For Elements State    ${report_Origin_Region}    
    Wait For Elements State    ${report_destination_Region}   
    Wait For Elements State    ${report_Origin_Cluster}   
    Wait For Elements State    ${report_destination_Cluster}   
    Wait For Elements State    ${report_origin_Country}    
    Wait For Elements State    ${report_destination_Country}  
    Wait For Elements State    ${report_origin_Airport}   
    Wait For Elements State    ${report_destination_Airport}  
    Wait For Elements State    ${report_origin_Gateway}   
    Wait For Elements State    ${report_destination_Gateway}   
    Wait For Elements State    ${report_service_Level}  
    Wait For Elements State    ${report_Reset}  


Step ${step} Select RFQ Launch Date From 2024 01 01 in Transit Time Report
    #************************Step 9******************************
    #  Do:
    #      Select RFQ Launch Date from 2024 01 01
    #      Click on Generate Report
    #  Expected:
    #      After a few seconds the file is downloaded like attached
    #************************************************************
    select options by    ${report_RFQ_Date_Type}     text    RFQ Launch Date
    Fill Text    ${report_RFQ_Date_From}    01/01/2024
    Fill Text    ${report_RFQ_Date_To}    31/12/2024
    ${DOWNLOAD_PATH}     Normalize Path    ${DOWNLOAD_PATH}
    ${report_download}    Set Variable    ${DOWNLOAD_PATH}${/}report_step9.xlsx
    Set Test Variable    ${report_download}    ${report_download}
    ${dl_promise}    Promise To Wait For Download    ${report_download}
    Wait Until Keyword Succeeds    10x    1s    Click     ${report_Generate_Report}
    Sleep    120s
    Wait Until Keyword Succeeds    20x    3s        Directory Should Not Be Empty    ${DOWNLOAD_PATH}
    Wait Until Keyword Succeeds    10x    1s    Copy File    ${DOWNLOAD_PATH}${/}*    ${report_download}
    Sleep    10s
    ${exp_report}    Set Variable    ${CURDIR}${/}TestData${/}Step9_AIR Transit Time Report_08162024_Oe.xlsx 
    Compare Excel Value    ${report_download}    ${exp_report}    17    2 
    Compare Excel Value    ${report_download}    ${exp_report}    20    36    startRow=19  
    Close All Excel Documents
    Empty Directory    ${DOWNLOAD_PATH}

Check All Check Box for Report
    [Arguments]    ${sectionClass}
    Wait Until Search Finished
    Wait Until Search Finished
    ${Tabindex}    Set Variable        1
    ${checkboxindex}    Set Variable        1
    ${uncheckedbox}    Set Variable    //section[@class="${sectionClass}"]/div//input[@type="checkbox"][contains(@class, 'ng-empty')]
    ${TabName}    Set Variable    //section[@class="${sectionClass}"]/div//li/a[@class='ng-binding']
    ${count}    Get Element Count    ${uncheckedbox}
    # ${count}    Set Variable    10
    ${TabCount}    Get Element Count    ${TabName}
    ${end}    Evaluate    int(${count}+1)
    # ${TabEnd}    Evaluate    int(${TabCount}+1)
    FOR    ${index}    IN RANGE    1    ${end}    1
        Log    ${index}
        ${originalcount}    Get Element Count    ${uncheckedbox}
        ${status}    Run Keyword And Return Status    Get Element States    (${uncheckedbox})[${checkboxindex}]    contains    visible
        IF    '${status}' == 'True'
            Click    (${uncheckedbox})[${checkboxindex}]
            ${currentcount}    Get Element Count    ${uncheckedbox}
            IF    ${currentcount} == ${originalcount}
                ${checkboxindex}    Evaluate    int(${checkboxindex}+1)
            END
        ELSE
            ${Tabindex}    Evaluate    int(${Tabindex}+1)
            Log    ${Tabindex}
            Click    (${TabName})[${Tabindex}]
            ${originalcount}    Get Element Count    ${uncheckedbox}
            ${visibleStatus}    Run Keyword And Return Status    Get Element States    (${uncheckedbox})[${checkboxindex}]    contains    visible
            IF    '${visibleStatus}' == 'True'
                    Click    (${uncheckedbox})[${checkboxindex}]
                    ${currentcount}    Get Element Count    ${uncheckedbox}
                    IF    ${currentcount} == ${originalcount}
                         ${checkboxindex}    Evaluate    int(${checkboxindex}+1)
                    END
            END
        END
        ${currentcount}    Get Element Count    ${uncheckedbox}
        IF    ${TabCount} == ${Tabindex}
            Exit For Loop
        END
    END

Check Search Result Count in Deadline Time Report
    ${count}    Browser.Get Element Count    //table[@class='table table-striped']//tbody/tr
    Should Not Be Equal As Integers    ${count}    0
